package b1.net
{
	import b1.conifg.*;
	import b1.players.*;
	import b1.scenes.*;
	import b1.scenes.fight.*;
	import b1.scenes.home.*;
	import b1.utils.*;
	
	import flash.errors.*;
	import flash.events.*;
	import flash.net.*;
	import flash.text.*;
	import flash.utils.*;

	//和后台服务器交互助手 
	public class ServerHelper extends EventDispatcher
	{
		//时限			 
		public const processTimeOut:int = 10;
		//唯一对象
		static private var instance:ServerHelper;		
		//正在处理标识符
		public var processingFlag:Boolean;	
		//上次请求的时间(单位:秒) 
		public var lastTriggerTime:int;
		//对话号
		public var sessionId:int;
		//回调函数
		private var callBackFunc:Function;
		//同步时的服务器时间(单位：秒）
		private var serverTime:int;
		//同步时的客户端时间（单位：秒）
		private var clientTime:int;
		 
		public function ServerHelper()
		{
			super();
			processingFlag = false;
			lastTriggerTime = 0;
			serverTime = 0;
			sessionId = 0;
		}
		
		public static function getInstance():ServerHelper
		{
			if (!instance)
				instance = new ServerHelper();
			return instance;
		}
		
		//对中文进行UTF编码转化
		private static function utf8Encode(string:String):String 
		{
			string = string.replace(/\r\n/g,'\n');
			string = string.replace(/\r/g,'\n');
			
			var utfString:String = "";
			
			for (var i:int = 0 ; i < string.length ; i++) 
			{
				var chr:Number = string.charCodeAt(i);
				if (chr < 128) 
				{
					utfString += String.fromCharCode(chr);
				}
				else if ((chr > 127) && (chr < 2048)) 
				{
					utfString += String.fromCharCode((chr >> 6) | 192);
					utfString += String.fromCharCode((chr & 63) | 128);
				}
				else 
				{
					utfString += String.fromCharCode((chr >> 12) | 224);
					utfString += String.fromCharCode(((chr >> 6) & 63) | 128);
					utfString += String.fromCharCode((chr & 63) | 128);
				}
			}
			return utfString;
		}

		public static function generateSig(variables:URLVariables, secret:String):String
		{
			var varArray:Array = new Array();
			for (var key:String in variables)
			{
				varArray.push(key + "=" + variables[key]);
			} 
			varArray.sort();
			var varStr:String = varArray.join("");
			varStr += secret;
			varStr = utf8Encode(varStr);
			return Md5.hash(varStr);
		}
		
		//模拟的当前的服务器系统时间(秒)
		public function now():int
		{
			return serverTime + getTimer() / 1000 - clientTime; 
		}
		
		private function handleIOError(event:IOErrorEvent):void
		{
			processingFlag = false;
			SceneContainer.getInstance().restoreSystemMouse();
			SceneContainer.getInstance().debug("ServerHellper IO Error: " + event.text);
		} 
		
		public function postRequest(webpage:String, variables:URLVariables, handleFunction:Function, callBackFunc:Function = null, force:Boolean = false):Boolean
		{
			var currentTime:int = now();
			if (!force && processingFlag && currentTime - lastTriggerTime < processTimeOut)
				return false;
			if (callBackFunc != null)
				this.callBackFunc = callBackFunc;
			if (currentTime <= lastTriggerTime)
				currentTime = lastTriggerTime + 1;
			if (PlayerInfoHelper.getInstance().userId >= 0)
				variables.userId = PlayerInfoHelper.getInstance().userId;
			else
				variables.renrenId = PlayerInfoHelper.getInstance().rrUserId;
			variables.sessionId = this.sessionId;
			variables.callId = currentTime;
			variables.b1_sig = generateSig(variables, Profile.serverSecret);
			var request:URLRequest = new URLRequest(webpage);
			request.method = URLRequestMethod.POST;
			request.data = variables;
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.BINARY;
			loader.addEventListener(Event.COMPLETE, handleFunction);
			loader.addEventListener(IOErrorEvent.IO_ERROR, handleIOError);
			SceneContainer.getInstance().customizeMouse();
			try
			{
				processingFlag = true;
				lastTriggerTime = currentTime;
				loader.load(request);
			}
			catch (error:Error)
			{
				processingFlag = false;
				SceneContainer.getInstance().restoreSystemMouse();
				trace("B1: Post request error occurred.");
			}
			return processingFlag;
		}
		
		//Process the common result
		private function processCommonResult(dataXML:XML):String
		{
			SceneContainer.getInstance().debug("processCommonResult: result=" + dataXML.result + ", debug=" + dataXML.debug + ". debug2=" + dataXML.debug2);
			processingFlag = false;
			SceneContainer.getInstance().restoreSystemMouse();
			serverTime = parseInt(dataXML.timeStamp);
			clientTime = getTimer() / 1000;
			var result:String = dataXML.result;
			if (result == "other login")
			{
				trace("用户已经在其他地方登陆！\n请重新登陆！");
			}
			if (result != "success" && Profile.DEBUG)
			{
				trace("错误: " + dataXML.toString());
			}
			var serverEvent:ServerEvent = new ServerEvent(dataXML.action);
			serverEvent.action = dataXML.action;
			serverEvent.result = dataXML.result;
			dispatchEvent(serverEvent);
			return result;
		} 
		
		public function syncMeFirst(callBack:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.syncMeFirst");
			var variables:URLVariables = new URLVariables();
			variables.action = "syncTime";
			return postRequest(Profile.URL_Action, variables, handleSyncTime, callBack);
		}
		
		private function handleSyncTime(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleSyncTime");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			getSessionId();
		}
		
		private function getSessionId():Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.getSessionId");
			var variables:URLVariables = new URLVariables();
			variables.action = "getSessionId";
			return postRequest(Profile.URL_Action, variables, handleGetSessionId);
		}
		
		private function handleGetSessionId(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleGetSessionId");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var userId:int = parseInt(dataXML.userId);
			PlayerInfoHelper.getInstance().userId = userId;
			this.sessionId = parseInt(dataXML.sessionId);
			if (callBackFunc != null)
				this.callBackFunc();
		}
		
		public function getTopAnimals(num:int, callBack:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.getTopAnimals");
			var variables:URLVariables = new URLVariables();
			variables.action = "getTopAnimals";
			variables.num = num;
			return postRequest(Profile.URL_Action, variables, handleGetTopAnimals,callBack);
		}
		
		private function handleGetTopAnimals(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleGetTopAnimals");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var players:Array = new Array();
			var len:int = dataXML.player.length();
			for (var i:int = 0; i < len; i++)
			{ 
				var parseId:int = parseInt(dataXML.player[i].playerId);
				var equipId:int;
				var equipLevel:int;
				var site:int = findPlayer(players, parseId);
				var player:PlayerInfo;
				if (site == -1)
				{
					player = new PlayerInfo();
					player.id = parseId;
					player.renrenId = parseInt(dataXML.player[i].renrenId);
					player.heirloomLevelCount = parseInt(dataXML.player[i].heirloomLevelCount);
//					player.heirloomLevelRank = parseInt(dataXML.player[i].heirloomLevelRank);
					players.push(player);
				}
				else
				{
					player = (players[site] as PlayerInfo);
				}
				equipId = parseInt(dataXML.player[i].equipId);
				equipLevel = parseInt(dataXML.player[i].equipLevel);
				player.playerHonour[equipId] = equipLevel; 
				player.equipScoreCount += equipLevel;
			}
			players.sort(sortEquipScore);
			for (i = 0; i < players.length; i++)
			{
				(players[i] as PlayerInfo).equipRank = i;
			}
			if (callBackFunc != null)
				this.callBackFunc(players);
		}
		
		private function sortEquipScore(playerInfo1:PlayerInfo, playerInfo2:PlayerInfo):int
		{ 
			if (playerInfo1.equipScoreCount > playerInfo2.equipScoreCount)
				return -1;
			else if (playerInfo1.equipScoreCount < playerInfo2.equipScoreCount)
				return 1;
			else
				return 0;
		}
		
		private function findPlayer(players:Array, target:int):int
		{
			for (var i:int = 0; i < players.length; i++)
			{
				if ((players[i] as PlayerInfo).id == target)
				{
					return i;
				}
			}
			return -1;
		}

		//得到所有人人好友
		public function getRrFriends(rrPlayerIds:Array, callBack:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.getRrFriends");
			var variables:URLVariables = new URLVariables();
			variables.action = "getRrFriends";
			variables.rrPlayerIds = rrPlayerIds[0];
			for (var i:int = 1; i < rrPlayerIds.length; i++)
			{
				variables.rrPlayerIds += "," + rrPlayerIds[i];
			}
			return postRequest(Profile.URL_Action, variables, handleGetRrFriends, callBack);
		}
		
		private function handleGetRrFriends(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleGetRrFriends");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var players:Object = PlayerInfoHelper.getInstance().players;
			var animals:Object = PlayerInfoHelper.getInstance().animals;
			var len:int = dataXML.liveAnimal.length();
			var playerInfo:PlayerInfo;
			for (var i:int = 0; i < len; i++)
			{ 
				var animal:AnimalInfo = new AnimalInfo(dataXML.liveAnimal[i]);
				playerInfo = players[animal.liveId];
				if (playerInfo == null)
				{
					playerInfo = new PlayerInfo();
					playerInfo.id = animal.liveId;
					playerInfo.renrenId = parseInt(dataXML.liveAnimal[i].ownerRrId);
					playerInfo.name = animal.ownerName;
					playerInfo.type = 2;
					playerInfo.ownAnimals = new Array();
					playerInfo.liveAnimals = new Array();
					players[playerInfo.id] = playerInfo;
				}
				if (animal.id > 0)
				{
					playerInfo.liveAnimals.push(animal.id);
					animals[animal.id] = animal;
				}
			}
			//装备信息
			for (i = 0; i < dataXML.equip.length(); i++)
			{
				var animalId:int = parseInt(dataXML.equip[i].animalId);
				var equip:EquipInfo = new EquipInfo(dataXML.equip[i]);
				animal = animals[animalId];
				animal.equips[equip.position] = equip;
			}
			if (callBackFunc != null)
				this.callBackFunc();
		}
		
		//得到自己和邻居
		public function getMeAndNeighbors(neighborNum:int, callBack:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.getMeAndNeighbors");
			var variables:URLVariables = new URLVariables();
			variables.action = "getMeAndNeighbors";
			variables.neighborNum = neighborNum;
			return postRequest(Profile.URL_Action, variables, handleGetMeAndNeighbors, callBack);
		}
		
		private function handleGetMeAndNeighbors(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleGetMeAndNeighbors");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var players:Object = PlayerInfoHelper.getInstance().players;
			var animals:Object = PlayerInfoHelper.getInstance().animals;
			var playerInfo:PlayerInfo = new PlayerInfo();
			//所有我拥有的动物
			var len:int = dataXML.ownAnimal.length();
			playerInfo.id = PlayerInfoHelper.getInstance().userId;
			playerInfo.renrenId = PlayerInfoHelper.getInstance().rrUserId;
			playerInfo.name = PlayerInfoHelper.getInstance().rrUserName;
			playerInfo.type = 0;
			playerInfo.ownAnimals = new Array();
			playerInfo.liveAnimals = new Array();
			players[playerInfo.id] = playerInfo;
			var animal:AnimalInfo;
			for (var i:int = 0; i < len; i++)
			{ 
				animal = new AnimalInfo(dataXML.ownAnimal[i]);
				if (animal.id > 0)
				{
					playerInfo.ownAnimals.push(animal.id);
					animals[animal.id] = animal;
				}
			}
			//所有居住在我家和我邻居家的动物
			len = dataXML.liveAnimal.length();
			for (i = 0; i < len; i++)
			{ 
				animal = new AnimalInfo(dataXML.liveAnimal[i]);
				playerInfo = players[animal.liveId];
				if (playerInfo == null)
				{
					playerInfo = new PlayerInfo();
					playerInfo.id = animal.liveId;
					playerInfo.renrenId = parseInt(dataXML.liveAnimal[i].ownerRrId);
					playerInfo.homeIndex = parseInt(dataXML.liveAnimal[i].homeIndex);
					playerInfo.name = animal.ownerName;
					if (playerInfo.id == PlayerInfoHelper.getInstance().userId)
						playerInfo.type = 0;
					else
						playerInfo.type = 1;
					playerInfo.ownAnimals = new Array();
					playerInfo.liveAnimals = new Array();
					players[playerInfo.id] = playerInfo;
				}
				if (animal.id > 0)
				{
					if (playerInfo.type == 0 || playerInfo.type == 1)
					{
						playerInfo.liveAnimals.push(animal.id);
						animals[animal.id] = animal;
					}
				}
			}
			//装备信息
			for (i = 0; i < dataXML.equip.length(); i++)
			{
				var animalId:int = parseInt(dataXML.equip[i].animalId);
				var equip:EquipInfo = new EquipInfo(dataXML.equip[i]);
				animal = animals[animalId];
				animal.equips[equip.position] = equip;
			}
			//我的信息
			var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			player.accomplishedTaskId = parseInt(dataXML.userInfo[0].accomplishedTaskId);
			player.accomplishedFestivalTaskId = parseInt(dataXML.userInfo[0].accomplishedFestivalTaskId);
			player.homeIndex = parseInt(dataXML.userInfo[0].homeIndex);
			player._heirloomBoxBoneLeft = parseInt(dataXML.userInfo[0].heirloomBoxBoneLeft);
			player.heirloomBoxFetchTime = parseInt(dataXML.userInfo[0].heirloomBoxFetchTime);
			player.heirloomLevelCount = parseInt(dataXML.userInfo[0].heirloomLevelCount);
//			player.heirloomLevelRank = parseInt(dataXML.userInfo[0].heirloomLevelRank);
			player.userPackage = new PackageInfo(dataXML.packageInfo[0]);
			var honourLength:int = dataXML.userInfo[0].honourInfo.length();
			for (i = 0; i < honourLength; i++)
			{
				var honourXML:XML = dataXML.userInfo[0].honourInfo[i];
				player.playerHonour[parseInt(honourXML.equipsId)] = parseInt(honourXML.equipsLevel);
			}
			if (callBackFunc != null)
				this.callBackFunc();
		}
		
		public function addUserOwnAnimal(userId:int, userName:String, homeIndex:int, animalIndex:int, animalName:String, experience:int, level:int, healthPoint:int, vitalityPoint:int, intelligence:int, speed:int, skillPoint:int, animalClues:Object, callBackFunc:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.addUserOwnAnimal");
			var variables:URLVariables = new URLVariables();
			variables.action = "addUserOwnAnimal";
			variables.userName = userName;
			variables.homeIndex = homeIndex;
			variables.animalIndex = animalIndex;
			variables.animalName = animalName;
			variables.experience = experience;
			variables.level = level;
			variables.healthPoint = healthPoint;
			variables.vitalityPoint = vitalityPoint;
			variables.intelligence = intelligence;
			variables.speed = speed;
			variables.skillPoint = skillPoint;
			variables.animalClues = "";
			for (var clueIndex:String in animalClues)
			{
				variables.animalClues += clueIndex + "," + animalClues[clueIndex] + ",";
			}
			return postRequest(Profile.URL_Action, variables, handleAddUserOwnAnimal, callBackFunc);
		}
		
		private function handleAddUserOwnAnimal(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleAddUserOwnAnimal");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var userId:int = parseInt(dataXML.userId);
			var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			var animals:Object = PlayerInfoHelper.getInstance().animals;
			var animal:AnimalInfo = new AnimalInfo(dataXML.animal[0]);
			animals[animal.id] = animal;
			player.ownAnimals.push(animal.id);
			if (callBackFunc != null)
				this.callBackFunc();
		}
		
		//开始侦探某个案件（设置标志符）
		public function setCaseDetected(caseId:int, ownerDetectedFlag:Boolean, otherDetectedFlag:Boolean, callBackFunc:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.setCaseDetected");
			var variables:URLVariables = new URLVariables();
			variables.action = "setCaseDetected";
			variables.caseId = caseId;
			variables.ownerDetectedFlag = ownerDetectedFlag;
			variables.otherDetectedFlag = otherDetectedFlag;
			return postRequest(Profile.URL_Action, variables, handleSetCaseDetected, callBackFunc);
		}
		
		private function handleSetCaseDetected(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleSetCaseDetected");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			if (callBackFunc != null)
				this.callBackFunc(dataXML.result);
		}
		
		//得到所有相关的案件
		public function getAllCases(detectiveAnimalId:int, callBackFunc:Function = null):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.getAllCases");
			var variables:URLVariables = new URLVariables();
			variables.action = "getAllCases";
			var players:Object = PlayerInfoHelper.getInstance().players;
			variables.detectiveAnimalId = detectiveAnimalId;
			variables.animalIds = "";
			for (var playerId:String in players)
			{
				var player:PlayerInfo = players[playerId];
				if (player.type == 0 || player.type == 1 || player.type == 2)
				{
					for (var i:int = 0; i < player.liveAnimals.length; i++)
					{
						variables.animalIds += player.liveAnimals[i] + ",";
					}
				}
			}
			return postRequest(Profile.URL_Action, variables, handleGetAllCases, callBackFunc);
		}
		
		//处理返回的所有案件
		private function handleGetAllCases(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleGetAllCases");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var battleCases:Object = new Object();
			var len:int = dataXML.clue.length();
			for (var i:int = 0; i < len; i++)
			{ 
				var clue:ClueInfo = new ClueInfo(dataXML.clue[i]);
				if (battleCases[clue.caseId] == null)
				{
					battleCases[clue.caseId] = new BattleCaseInfo(dataXML.clue[i]);
				}
				(battleCases[clue.caseId] as BattleCaseInfo).clues.push(clue);
			}
			PlayerInfoHelper.getInstance().battleCases = battleCases;
			if (callBackFunc != null)
				this.callBackFunc();
		}
		
		//得到我被立案的所有案件
		public function getMyCases(callBackFunc:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.getMyCases");
			var variables:URLVariables = new URLVariables();
			variables.action = "getMyCases";
			var me:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			variables.animalId = me.ownAnimals[0];
			return postRequest(Profile.URL_Action, variables, handleGetMyCases, callBackFunc);
		}
		
		//处理我被立案的所有案件
		private function handleGetMyCases(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleGetMyCases");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var battleCases:Object = new Object();
			var len:int = dataXML.clue.length();
			for (var i:int = 0; i < len; i++)
			{ 
				var clue:ClueInfo = new ClueInfo(dataXML.clue[i]);
				if (battleCases[clue.caseId] == null)
				{
					battleCases[clue.caseId] = new BattleCaseInfo(dataXML.clue[i]);
				}
				(battleCases[clue.caseId] as BattleCaseInfo).clues.push(clue);
			}
			if (callBackFunc != null)
				this.callBackFunc(battleCases);
		}
		
		//立案
		public function addCase(winnerAnimalId:int, loserAnimalId:int, caseBones:int, clues:Object, boneBalance:String, loseHeirloomId:int, callBackFunc:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.addCase");
			var variables:URLVariables = new URLVariables();
			variables.action = "addCase";
			variables.winnerAnimalId = winnerAnimalId;
			variables.loserAnimalId = loserAnimalId;
			variables.caseBones = caseBones;
			variables.boneBalance = boneBalance;
			variables.clueInfo = "";
			variables.loseHeirloomId = loseHeirloomId;
			for (var clueIndex:String in clues)
			{
				var clueInfo:ClueInfo = clues[clueIndex];
				variables.clueInfo += clueInfo.clueIndex + ", " + clueInfo.clueValue + ", " + clueInfo.timeToDisappear + ", ";
			}
			return postRequest(Profile.URL_Action, variables, handleAddCase, callBackFunc);
		}
		
		private function handleAddCase(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleAddCase");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			player.userPackage = new PackageInfo(dataXML.packageInfo[0]);
			var battleCase:BattleCaseInfo = new BattleCaseInfo();
			var len:int = dataXML.clue.length();
			for (var i:int = 0; i < len; i++)
			{ 
				var clue:ClueInfo = new ClueInfo(dataXML.clue[i]);
				battleCase.clues.push(clue);
			}
			battleCase.caseId = parseInt(dataXML.caseId);
			battleCase.loseHeirloomId = parseInt(dataXML.loseHeirloomId);
			battleCase.caseBones = parseInt(dataXML.caseBones);
			if (callBackFunc != null)
				this.callBackFunc(dataXML.result, battleCase);
		}
		
		//取下一个嫌疑犯
		public function getRandomSuspect(caseId:int, clues:Array, magicStr:String, callBackFunc:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.getRandomSuspect");
			var variables:URLVariables = new URLVariables();
			variables.action = "getRandomSuspect";
			variables.caseId = caseId;
			variables.clues = "";
			for (var i:int = 0; i < clues.length; i++)
			{
				var clueInfo:ClueInfo = clues[i];
				variables.clues += clueInfo.clueIndex + "," + clueInfo.clueValue + ",";
			}
			variables.t = now();
			variables.magicKey = Md5.hash(variables.t + magicStr);
			return postRequest(Profile.URL_Action, variables, handleGetRandomSuspect, callBackFunc);
		}
		
		private function handleGetRandomSuspect(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleGetRandomSuspect");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var animal:AnimalInfo = new AnimalInfo(dataXML.animal[0]);
			PlayerInfoHelper.getInstance().animals[animal.id] = animal;
			if (callBackFunc != null)
				this.callBackFunc(animal.id);
		}
		
		//收获传家宝箱信息
		public function fetchHeirloomBox(targetUserId:int, getBones:int, leftBones:int, callBack:Function = null):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.fetchHeirloomBox");
			var variables:URLVariables = new URLVariables();
			variables.action = "fetchHeirloomBox";
			variables.targetUserId = targetUserId;
			variables.getBones = getBones;
			variables.leftBones = leftBones;
			var boneBalance:String = PlayerInfoHelper.getInstance().userId + ",0," + getBones;
			variables.boneBalance = boneBalance;
			return postRequest(Profile.URL_Action, variables, handleFetchHeirloomBox, callBack);
		}
		
		private function handleFetchHeirloomBox(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleFetchHeirloomBox");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			for (var i:int = 0; i < dataXML.packageInfo.length(); i++)
			{
				var playerId:int = parseInt(dataXML.packageInfo[i].object[0].userId);
				var player:PlayerInfo = PlayerInfoHelper.getInstance().players[playerId];
				player.userPackage = new PackageInfo(dataXML.packageInfo[i]);
			}
			var getBones:int = parseInt(dataXML.getBones);
			var targetUserId:int = parseInt(dataXML.targetUserId);
			var targetUser:PlayerInfo = PlayerInfoHelper.getInstance().players[targetUserId];
			targetUser._heirloomBoxBoneLeft = parseInt(dataXML.leftBones);
			targetUser.heirloomBoxFetchTime = parseInt(dataXML.timeStamp);
			if (callBackFunc != null)
			{
				this.callBackFunc(dataXML.result, getBones);
			}
		}
		
		//切换家
		public function switchHome(oldLiveId:int, newLiveId:int, callBack:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.switchHome");
			var variables:URLVariables = new URLVariables();
			variables.action = "switchHome";
			variables.oldLiveId = oldLiveId;
			variables.newLiveId = newLiveId;
			return postRequest(Profile.URL_Action, variables, handleSwitchHome, callBack);
		}
		
		private function handleSwitchHome(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleSwitchHome");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			if (dataXML.result == "success")
			{
				var newAnimalIds:Array = new Array();
				var players:Object = PlayerInfoHelper.getInstance().players;
				var oldLiveId:int = parseInt(dataXML.oldLiveId);
				var newLiveId:int = parseInt(dataXML.newLiveId);
				//新地方原籍动物的种类（为了得到camp值)
				var newLiveUserName:String = dataXML.newLiveUserName;
				var newLiveRenrenId:int = parseInt(dataXML.newLiveRenrenId);
				var newLiveHomeIndex:int = parseInt(dataXML.newLiveHomeIndex);
				if (players[newLiveId] == null)
				{
					players[newLiveId] = new PlayerInfo();
				}
				players[newLiveId].id = newLiveId;
				players[newLiveId].name = dataXML.newLiveUserName;
				players[newLiveId].renrenId = parseInt(dataXML.newLiveRenrenId);
				players[newLiveId].heirloomLevelCount = parseInt(dataXML.heirloomLevelCount);	
//				players[newLiveId].heirloomLevelRank = parseInt(dataXML.heirloomLevelRank);	
				players[newLiveId].heirloomBoxFetchTime = parseInt(dataXML.heirloomBoxFetchTime);	
				players[newLiveId]._heirloomBoxBoneLeft = parseInt(dataXML.heirloomBoxBoneLeft);
				players[newLiveId].equipRank = parseInt(dataXML.equipRank);
				//解析荣誉
				var honourLength:int = dataXML.honourInfo.length();
				for (i = 0; i < honourLength; i++)
				{
					var honourXML:XML = dataXML.honourInfo[i];
					players[newLiveId].playerHonour[parseInt(honourXML.equipsId)] = parseInt(honourXML.equipsLevel);
				}
				//解析动物
				if (oldLiveId > 0)
					players[oldLiveId].liveAnimals = new Array();
				players[newLiveId].liveAnimals = new Array();
				var animals:Object = PlayerInfoHelper.getInstance().animals;
				var len:int = dataXML.animal.length();
				for (var i:int = 0; i < len; i++)
				{ 
					var animal:AnimalInfo = new AnimalInfo(dataXML.animal[i]);
					players[animal.liveId].liveAnimals.push(animal.id);
					animals[animal.id] = animal;
					newAnimalIds.push(animal.id);
				}
				
				for ( i = 0; i < dataXML.arrestInfo.length(); i++)
				{
					var animalId:int = parseInt(dataXML.arrestInfo[i].animalId);
					var wantedBones:int = parseInt(dataXML.arrestInfo[i].wantedBones);
					var timeToClose:int = parseInt(dataXML.arrestInfo[i].timeToClose);
					(animals[animalId] as AnimalInfo).wantedBones += wantedBones;	
					if((animals[animalId] as AnimalInfo).timeToClose < timeToClose) 
						(animals[animalId] as AnimalInfo).timeToClose = timeToClose;
				}
				
				players[newLiveId].heirlooms = new Object();
				for (i = 0 ; i < dataXML.heirloom.length(); i++)
				{
					var tempHeir:Heirloom = new Heirloom();
					tempHeir.originalOwnerPlayerId = parseInt(dataXML.heirloom[i].originalOwnerPlayerId);
					tempHeir.originalPlayerName = dataXML.heirloom[i].originalPlayerName;
					tempHeir.currentOwnerPlayerId = dataXML.heirloom[i].currentOwnerPlayerId;
					tempHeir.level = dataXML.heirloom[i].heirloomLevel;
					(players[newLiveId] as PlayerInfo).heirlooms[tempHeir.originalOwnerPlayerId] = tempHeir;
				}
			}
			if (callBackFunc != null)
				this.callBackFunc(dataXML.result, oldLiveId, newLiveId, newLiveUserName, newLiveRenrenId, newLiveHomeIndex, newAnimalIds);
		}
		
		//获得包裹信息
		public function getPackageInfo(playerIds:Array, callBackFunc:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.getPackageInfo");
			var variables:URLVariables = new URLVariables();
			variables.action = "getPackageInfo";
			variables.playerIds = "";
			for (var i:int = 0; i < playerIds.length; i++)
			{
				variables.playerIds += playerIds[i] + ",";
			}
			return postRequest(Profile.URL_Action, variables, handleGetPackageInfo, callBackFunc);
		}

		private function handleGetPackageInfo(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleGetPackageInfo");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			for (var i:int = 0; i < dataXML.packageInfo.length(); i++)
			{
				var playerId:int = parseInt(dataXML.packageInfo[i].object[0].userId);
				var player:PlayerInfo = PlayerInfoHelper.getInstance().players[playerId];
				player.userPackage = new PackageInfo(dataXML.packageInfo[i]);
			}
			if (callBackFunc != null)
				this.callBackFunc(dataXML.result);
		}
		
		//购买物品
		public function purchaseObject(objectIndex:int, objectNum:int, priceBones:int, priceGoldBones:int, callBackFunc:Function, honourBalance:String = null):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.purchaseObject");
			var variables:URLVariables = new URLVariables();
			variables.action = "purchaseObject";
			variables.objectIndex = objectIndex;
			variables.objectNum = objectNum;
			variables.priceBones = priceBones;
			variables.priceGoldBones = priceGoldBones;
			variables.honourBalance = honourBalance;
			return postRequest(Profile.URL_Action, variables, handlePurchaseObject, callBackFunc);
		}

		private function handlePurchaseObject(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handlePurchaseObject");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			player.userPackage = new PackageInfo(dataXML.packageInfo[0]);
			//新的勋章
			var newHonours:Object = null;
			if (dataXML.newHonour.length() > 0)
			{
				newHonours = new Object();
				for (var i:int = 0; i < dataXML.newHonour.length(); i++)
				{
					var equipId:int = parseInt(dataXML.newHonour[i].equipId);
					var equipScore:int = parseInt(dataXML.newHonour[i].equipScore);
					newHonours[equipId] = equipScore;
	        		player.playerHonour[equipId] = equipScore;
				}
			}
			if (callBackFunc != null)
				this.callBackFunc(dataXML.result, newHonours);
		}
		
		//出售物品
		public function sellObject(objectIndex:int, objectNum:int, priceBones:int, priceGoldBones:int, callBackFunc:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.sellObject");
			var variables:URLVariables = new URLVariables();
			variables.action = "sellObject";
			variables.objectIndex = objectIndex;
			variables.objectNum = objectNum;
			variables.priceBones = priceBones;
			variables.priceGoldBones = priceGoldBones;
			return postRequest(Profile.URL_Action, variables, handleSellObject, callBackFunc);
		}

		private function handleSellObject(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleSellObject");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			player.userPackage = new PackageInfo(dataXML.packageInfo[0]);
			if (callBackFunc != null)
				this.callBackFunc(dataXML.result);
		}
		
		//指认犯人
		public function arrestAnimal(detectiveAnimalId:int, arrestTargetAnimalId:int, timeToGetFree:int, boneBalance:String, expBalance:String, heirloomBalance:String, callBackFunc:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.arrestAnimal");
			var variables:URLVariables = new URLVariables();
			variables.action = "arrestAnimal";
			variables.detectiveAnimalId = detectiveAnimalId;
			variables.arrestTargetAnimalId = arrestTargetAnimalId;
			variables.timeToGetFree = timeToGetFree;
			variables.heirloomBalance = heirloomBalance;
			variables.boneBalance = boneBalance;
			variables.expBalance = expBalance;
			return postRequest(Profile.URL_Action, variables, handleArrestAnimal, callBackFunc);
		}

		private function handleArrestAnimal(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleArrestAnimal");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var arrestTargetAnimalId:int = parseInt(dataXML.arrestTargetAnimalId);
			var timeToGetFree:int = parseInt(dataXML.timeToGetFree);
			var arrestTargetAnimal:AnimalInfo = PlayerInfoHelper.getInstance().animals[arrestTargetAnimalId];
			arrestTargetAnimal.timeToGetFree = timeToGetFree;
			var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			player.userPackage = new PackageInfo(dataXML.packageInfo[0]);
			
			var tempHeir:Heirloom = new Heirloom();
			tempHeir.originalOwnerPlayerId = parseInt(dataXML.heirloomInfo.originalOwnerPlayerId);
			tempHeir.originalPlayerName = dataXML.heirloomInfo.heirloomOwnerName;
			tempHeir.currentOwnerPlayerId = parseInt(dataXML.heirloomInfo.currentOwnerPlayerId);
			player.heirlooms[tempHeir.originalOwnerPlayerId] = tempHeir;
			
			var arrestPlayer:PlayerInfo = PlayerInfoHelper.getInstance().players[arrestTargetAnimal.ownerId];
			for(var heirIndex:String in arrestPlayer.heirlooms)
			{
				if(arrestPlayer.heirlooms[heirIndex] == null) continue;
				if((arrestPlayer.heirlooms[heirIndex] as Heirloom).originalOwnerPlayerId == tempHeir.originalOwnerPlayerId)
				{
					arrestPlayer.heirlooms[heirIndex] = null;
				}
			}
			
			var animal:AnimalInfo = new AnimalInfo(dataXML.animal[0]);
			PlayerInfoHelper.getInstance().animals[animal.id] = animal;
			if (callBackFunc != null)
				this.callBackFunc(dataXML.result);
		}
		
		//指认案件主犯
		public function identifySuspectAnimal(caseId:int, detectiveOwnerId:int, suspectAnimalId:int, claimBones:int, callBackFunc:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.identifySuspectAnimal");
			var variables:URLVariables = new URLVariables();
			variables.action = "identifySuspectAnimal";
			variables.caseId = caseId;
			variables.detectiveOwnerId = detectiveOwnerId;
			variables.suspectAnimalId = suspectAnimalId;
			variables.claimBones = claimBones;
			return postRequest(Profile.URL_Action, variables, handleIdentifySuspectAnimal, callBackFunc);
		}

		private function handleIdentifySuspectAnimal(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleIdentifySuspectAnimal");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			player.userPackage = new PackageInfo(dataXML.packageInfo[0]);
			if (callBackFunc != null)
				this.callBackFunc(dataXML.result);
		}
		
		//指认通缉的犯人
		public function identifyWantedAnimal(detectiveOwnerId:int, suspectAnimalId:int, claimBones:int, callBackFunc:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.identifyWantedAnimal");
			var variables:URLVariables = new URLVariables();
			variables.action = "identifyWantedAnimal";
			variables.detectiveOwnerId = detectiveOwnerId;
			variables.suspectAnimalId = suspectAnimalId;
			variables.claimBones = claimBones;
			return postRequest(Profile.URL_Action, variables, handleIdentifyWantedAnimal, callBackFunc);
		}

		private function handleIdentifyWantedAnimal(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleIdentifyWantedAnimal");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var wantedBonesSum:int = parseInt(dataXML.wantedBonesSum);
			var len:int = dataXML.battleCase.length();
			var battleCases:Array = new Array();
			for (var i:int = 0; i < len; i++)
			{ 
				battleCases.push(new BattleCaseInfo(dataXML.battleCase[i]));
			}
			var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			player.userPackage = new PackageInfo(dataXML.packageInfo[0]);
			if (callBackFunc != null)
				this.callBackFunc(dataXML.result, wantedBonesSum, battleCases);
		}
		
		//发布通缉
		public function postWantedAnimal(caseId:int, wantedBones:int, timeToClose:int, callBackFunc:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.postWantedAnimal");
			var variables:URLVariables = new URLVariables();
			variables.action = "postWantedAnimal";
			variables.caseId = caseId;
			variables.wantedBones = wantedBones;
			variables.timeToClose = timeToClose;
			return postRequest(Profile.URL_Action, variables, handlePostWantedAnimal, callBackFunc);
		}
		
		private function handlePostWantedAnimal(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handlePostWantedAnimal");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			if (callBackFunc != null)
				this.callBackFunc(dataXML.result);
		}
		
		//动物跑路
		public function runAwayAnimal(animalId:int, targetLiveId:int, bonesForRunAway:int, callBackFunc:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.runAwayAnimal");
			var variables:URLVariables = new URLVariables();
			variables.action = "runAwayAnimal";
			variables.animalId = animalId;
			variables.targetLiveId = targetLiveId;
			variables.boneBalance = PlayerInfoHelper.getInstance().userId + ",0," + (-bonesForRunAway);
			return postRequest(Profile.URL_Action, variables, handleRunAwayAnimal, callBackFunc);
		}
		
		private function handleRunAwayAnimal(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleRunAwayAnimal");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			player.userPackage = new PackageInfo(dataXML.packageInfo[0]);
			if (callBackFunc != null)
				this.callBackFunc(dataXML.result);
		}
		
		//保释动物
		public function bailAnimal(animalId:int, bailBones:int, callBackFunc:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.bailAnimal");
			var variables:URLVariables = new URLVariables();
			variables.action = "bailAnimal";
			variables.animalId = animalId;
			variables.bailBones = bailBones;
			return postRequest(Profile.URL_Action, variables, handleBailAnimal, callBackFunc);
		}
		
		private function handleBailAnimal(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleBailAnimal");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var animalId:int = -1;
			if (dataXML.result == "success")
			{
				animalId = parseInt(dataXML.animalId);
				var animalInfo:AnimalInfo = PlayerInfoHelper.getInstance().animals[animalId];
				animalInfo.timeToGetFree = 0;
			}
			var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			player.userPackage = new PackageInfo(dataXML.packageInfo[0]);
			if (callBackFunc != null)
				this.callBackFunc(dataXML.result, animalId);
		}
		
		//开始作案
		public function startFight(leftPlayerId:int, rightPlayerId:int, leftAnimalIds:Array, rightAnimalIds:Array, callBackFunc:Function = null):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.startFight: " + leftPlayerId + ", rightPlayerId " + rightPlayerId);
			var variables:URLVariables = new URLVariables();
			variables.action = "startFight";
			variables.fightOfTime = Config.duration;
			variables.leftPlayerId = leftPlayerId; 
			variables.rightPlayerId = rightPlayerId;
			variables.leftAnimalIds = leftAnimalIds[0];
			for (var i:int = 1; i < leftAnimalIds.length; i++)
			{
				variables.leftAnimalIds += "," + leftAnimalIds[i];
			}
			variables.rightAnimalIds = rightAnimalIds[0];
			for (i = 1; i < rightAnimalIds.length; i++)
			{
				variables.rightAnimalIds += "," + rightAnimalIds[i];
			}
			return postRequest(Profile.URL_Action, variables, handleStartFight, callBackFunc);
		}

		private function handleStartFight(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleStartFight: " + event.toString());
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var leftPlayerId:int = parseInt(dataXML.leftPlayerId);
			var rightPlayerId:int = parseInt(dataXML.rightPlayerId);
			var leftAnimalIdStrs:Array = dataXML.leftAnimalIds.split(",");
			var rightAnimalIdStrs:Array = dataXML.rightAnimalIds.split(",");
			var leftAnimalIds:Array = new Array();
			var rightAnimalIds:Array = new Array();
			for (var i:int = 0; i < leftAnimalIdStrs.length; i++)
			{
				leftAnimalIds.push(parseInt(leftAnimalIdStrs[i]));
			}
			for (i = 0; i < rightAnimalIdStrs.length; i++)
			{
				rightAnimalIds.push(parseInt(rightAnimalIdStrs[i]));
			}
			if (dataXML.result == "success")
			{
				for (i = 0; i < dataXML.packageInfo.length(); i++)
				{
					var playerId:int = parseInt(dataXML.packageInfo[i].object[0].userId);
					var player:PlayerInfo = PlayerInfoHelper.getInstance().players[playerId];
					player.userPackage = new PackageInfo(dataXML.packageInfo[i]);
					for (var j:int = 0; j < dataXML.packageInfo[i].heirloom.length(); j++)
					{
						var tempHeir:Heirloom = new Heirloom();
						tempHeir.originalOwnerPlayerId = parseInt(dataXML.packageInfo[i].heirloom[j].originalOwnerPlayerId);
						tempHeir.currentOwnerPlayerId = parseInt(dataXML.packageInfo[i].heirloom[j].currentOwnerPlayerId);
						tempHeir.originalPlayerName = dataXML.packageInfo[i].heirloom[j].originalPlayerName;
						tempHeir.level = parseInt(dataXML.packageInfo[i].heirloom[j].heirloomLevel);
						player.heirlooms[tempHeir.originalOwnerPlayerId] = tempHeir;
					}
				}
				var animals:Object = PlayerInfoHelper.getInstance().animals;
				var len:int = dataXML.animal.length();
				for (i = 0; i < len; i++)
				{ 
					var animal:AnimalInfo = new AnimalInfo(dataXML.animal[i]);
					animals[animal.id] = animal;
				}
			}
			if (callBackFunc != null)
				this.callBackFunc(dataXML.result, leftPlayerId, rightPlayerId, leftAnimalIds, rightAnimalIds);
		}
		
		//结束作案
		public function endFight(leftPlayerId:int, rightPlayerId:int, leftAnimalIds:Array, rightAnimalIds:Array, hpBalance:String, boneBalance:String, expBalance:String, heirloomBalance:String, honourBalance:String = null,callBackFunc:Function = null):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.endFight: " + leftPlayerId + ", rightPlayerId " + rightPlayerId);
			var variables:URLVariables = new URLVariables();
			variables.action = "endFight";
			variables.leftPlayerId = leftPlayerId; 
			variables.rightPlayerId = rightPlayerId;
			variables.leftAnimalIds = leftAnimalIds[0];
			variables.hpBalance = hpBalance;
			variables.boneBalance = boneBalance;
			variables.expBalance = expBalance;
			variables.honourBalance = honourBalance;
			variables.heirloomBalance = heirloomBalance;
			for (var i:int = 1; i < leftAnimalIds.length; i++)
			{
				variables.leftAnimalIds += "," + leftAnimalIds[i];
			}
			variables.rightAnimalIds = rightAnimalIds[0];
			for (i = 1; i < rightAnimalIds.length; i++)
			{
				variables.rightAnimalIds += "," + rightAnimalIds[i];
			}
			return postRequest(Profile.URL_Action, variables, handleEndFight, callBackFunc);
		}

		private function handleEndFight(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleEndFight: " + event.toString());
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			for (var i:int = 0; i < dataXML.packageInfo.length(); i++)
			{
				var playerId:int = parseInt(dataXML.packageInfo[i].object[0].userId);
				var player:PlayerInfo = PlayerInfoHelper.getInstance().players[playerId];
				player.userPackage = new PackageInfo(dataXML.packageInfo[i]);
			}
			var animals:Object = PlayerInfoHelper.getInstance().animals;
			var len:int = dataXML.animal.length();
			for (i = 0; i < len; i++)
			{ 
				var animal:AnimalInfo = new AnimalInfo(dataXML.animal[i]);
				animals[animal.id] = animal;
			}
			//新的勋章
			var newHonours:Object = null;
			if (dataXML.newHonour.length() > 0)
			{
				newHonours = new Object();
				for (i = 0; i < dataXML.newHonour.length(); i++)
				{
					var equipId:int = parseInt(dataXML.newHonour[i].equipId);
					var equipScore:int = parseInt(dataXML.newHonour[i].equipScore);
					newHonours[equipId] = equipScore;
	        		player.playerHonour[equipId] = equipScore;
				}
			}
			if (callBackFunc != null)
				this.callBackFunc(newHonours);
		}
		
		public function balanceSkillPoint(animalId:int, addSkillPoint:int, addHealthPoint:int, addVitalityPoint:int, addIntelligence:int, addSpeed:int, callBackFunc:Function = null):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.balanceSkillPoint");
			var variables:URLVariables = new URLVariables();
			variables.action = "balanceSkillPoint";
			variables.animalId = animalId; 
			variables.addSkillPoint = addSkillPoint; 
			variables.addHealthPoint = addHealthPoint; 
			variables.addVitalityPoint = addVitalityPoint; 
			variables.addIntelligence = addIntelligence; 
			variables.addSpeed = addSpeed; 
			return postRequest(Profile.URL_Action, variables, handleBalanceSkillPoint, callBackFunc);
		}

		private function handleBalanceSkillPoint(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleBalanceSkillPoint");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var animal:AnimalInfo = new AnimalInfo(dataXML.animal[0]);
			PlayerInfoHelper.getInstance().animals[animal.id] = animal;
			if (callBackFunc != null)
				this.callBackFunc(dataXML.result);
		}
		
		public function balance(hpBalance:String, boneBalance:String, expBalance:String, callBackFunc:Function = null):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.balance");
			var variables:URLVariables = new URLVariables();
			variables.action = "balance";
			variables.hpBalance = hpBalance; 
			variables.boneBalance = boneBalance; 
			variables.expBalance = expBalance; 
			return postRequest(Profile.URL_Action, variables, handleBalance, callBackFunc);
		}

		private function handleBalance(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleBalance");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			for (var i:int = 0; i < dataXML.packageInfo.length(); i++)
			{
				var playerId:int = parseInt(dataXML.packageInfo[i].object[0].userId);
				var player:PlayerInfo = PlayerInfoHelper.getInstance().players[playerId];
				player.userPackage = new PackageInfo(dataXML.packageInfo[i]);
			}
			var animals:Object = PlayerInfoHelper.getInstance().animals;
			var len:int = dataXML.animal.length();
			for (i = 0; i < len; i++)
			{ 
				var animal:AnimalInfo = new AnimalInfo(dataXML.animal[i]);
				animals[animal.id] = animal;
			}
			if (callBackFunc != null)
				this.callBackFunc(dataXML.result);
		}
		
		public function accomplishTask(taskType:int, taskId:int):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.accomplishTask");
			var variables:URLVariables = new URLVariables();
			variables.action = "accomplishTask";
			variables.taskType = taskType;
			var taskConfig:TaskConfig;
			if (taskType == 0)
				taskConfig = Config.taskConfigs[taskId];
			else
				taskConfig = Config.festivalTaskConfigs[taskId];
			var me:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			var myAnimal:AnimalInfo = PlayerInfoHelper.getInstance().animals[me.ownAnimals[0]];
			//结算骨头
			variables.boneBalance = PlayerInfoHelper.getInstance().userId + ",0," + taskConfig.boneBonus; 
			//结算经验 
			var levelExp:Array = AnimalInfo.getLevelByAddExp(myAnimal.level, myAnimal.experience, taskConfig.expBonus);
			var expBalance:String = myAnimal.id + "," + levelExp[0] + "," + taskConfig.expBonus + "," + Config.skillPointsPerLevel;
			variables.expBalance = expBalance; 
			return postRequest(Profile.URL_Action, variables, handleAccomplishTask);
		}

		private function handleAccomplishTask(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleAccomplishTask");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			if (dataXML.result == "success")
			{
				var me:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
				var taskType:int = parseInt(dataXML.taskType);
				var taskConfig:TaskConfig; 
				//主线任务
				if (taskType == 0)
				{
					me.accomplishedTaskId++;
					taskConfig = Config.taskConfigs[me.accomplishedTaskId];
				}
				//节日任务
				else
				{
					me.accomplishedFestivalTaskId++;
					taskConfig = Config.festivalTaskConfigs[me.accomplishedFestivalTaskId];
				}
				for (var i:int = 0; i < dataXML.packageInfo.length(); i++)
				{
					var playerId:int = parseInt(dataXML.packageInfo[i].object[0].userId);
					var player:PlayerInfo = PlayerInfoHelper.getInstance().players[playerId];
					player.userPackage = new PackageInfo(dataXML.packageInfo[i]);
				}
				var animals:Object = PlayerInfoHelper.getInstance().animals;
				var len:int = dataXML.animal.length();
				for (i = 0; i < len; i++)
				{ 
					var animal:AnimalInfo = new AnimalInfo(dataXML.animal[i]);
					animals[animal.id] = animal;
				}
				if (SceneContainer.getInstance().currentScene is Home)
				{
					var home:Home = SceneContainer.getInstance().currentScene as Home;
					TaskAccomplishDialog.getInstance().open(home, taskConfig);
					BottomNavigator.getInstance().refresh();
				}
			}
		}
		
		//为动物穿装备
		public function getOnEquip(animalId:int, position:int, objectIndex:int, durability:int, oldObjectIndex:int, oldDurability:int, callBackFunc:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.getOnEquip");
			var variables:URLVariables = new URLVariables();
			variables.action = "getOnEquip";
			variables.animalId = animalId;
			variables.position = position;
			variables.objectIndex = objectIndex;
			variables.durability = durability;
			variables.boneBalance = PlayerInfoHelper.getInstance().userId + "," + objectIndex + ",-1";
			//如果老装备是永久物品，脱下后重新放入包裹
			if (oldDurability == -1)
			{
				variables.boneBalance += "," + PlayerInfoHelper.getInstance().userId + "," + oldObjectIndex + ",1";
			}
			return postRequest(Profile.URL_Action, variables, handleGetOnEquip, callBackFunc);
		}
		
		private function handleGetOnEquip(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleGetOnEquip");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var animalId:int = -1;
			if (dataXML.result == "success")
			{
				var animals:Object = PlayerInfoHelper.getInstance().animals;
				var len:int = dataXML.animal.length();
				for (var i:int = 0; i < len; i++)
				{ 
					var animal:AnimalInfo = new AnimalInfo(dataXML.animal[i]);
					animals[animal.id] = animal;
				}
				var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
				player.userPackage = new PackageInfo(dataXML.packageInfo[0]);
			}
			if (callBackFunc != null)
				this.callBackFunc(dataXML.result);
		}
		
		//为动物脱下装备
		public function getOffEquip(animalId:int, position:int, equipId:int, objectIndex:int, durability:int, callBackFunc:Function):Boolean
		{
			SceneContainer.getInstance().debug("ServerHelper.getOffEquip");
			var variables:URLVariables = new URLVariables();
			variables.action = "getOffEquip";
			variables.animalId = animalId;
			variables.position = position;
			variables.equipId = equipId;
			variables.objectIndex = objectIndex;
			variables.durability = durability;
			//如果是永久物品，重新放回包裹
			if (durability == -1)
				variables.boneBalance = PlayerInfoHelper.getInstance().userId + "," + objectIndex + ",1";
			return postRequest(Profile.URL_Action, variables, handleGetOffEquip, callBackFunc);
		}
		
		private function handleGetOffEquip(event:Event):void
		{
			SceneContainer.getInstance().debug("ServerHelper.handleGetOffEquip");
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var animalId:int = -1;
			if (dataXML.result == "success")
			{
				var animals:Object = PlayerInfoHelper.getInstance().animals;
				var len:int = dataXML.animal.length();
				for (var i:int = 0; i < len; i++)
				{ 
					var animal:AnimalInfo = new AnimalInfo(dataXML.animal[i]);
					animals[animal.id] = animal;
				}
				if (dataXML.packageInfo.length() > 0)
				{
					var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
					player.userPackage = new PackageInfo(dataXML.packageInfo[0]);
				}
			}
			if (callBackFunc != null)
				this.callBackFunc(dataXML.result);
		}
		
		////寻找拥有传家骨头的犯人的位置
		public function findCrimerSite(playerId:int, boneBalance:String, callBackFunc:Function):Boolean
		{
			var variables:URLVariables = new URLVariables();
			variables.action = "findCrimerSite";
			variables.playerId = playerId;
			variables.boneBalance = boneBalance;
			return postRequest(Profile.URL_Action, variables, handleFindCrimerSite, callBackFunc);
		}
		
		//寻找拥有传家骨头的犯人的位置
		private function handleFindCrimerSite(event:Event):void
		{
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var animals:Object = PlayerInfoHelper.getInstance().animals;
			var len:int = dataXML.animal.length();
			for (var i:int = 0; i < len; i++)
			{ 
				var animal:AnimalInfo = new AnimalInfo(dataXML.animal[i]);
				animals[animal.id] = animal;
			}
			if (dataXML.packageInfo.length() > 0)
			{
				var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
				player.userPackage = new PackageInfo(dataXML.packageInfo[0]);
			}
			var crimerLiveId:int = parseInt(dataXML.crimerLiveIds);
			BottomNavigator.getInstance().refresh();
			if (callBackFunc != null)
				this.callBackFunc(crimerLiveId);
		}
		
		//寻找传家骨头的位置
		public function findCrimerHome(playerId:int, boneBalance:String, callBackFunc:Function):Boolean
		{
			var variables:URLVariables = new URLVariables();
			variables.action = "findCrimerHome";
			variables.playerId = playerId;
			variables.boneBalance = boneBalance;
			return postRequest(Profile.URL_Action, variables, handleFindCrimerHome, callBackFunc);
		}
		
		//处理寻找传家骨头的位置
		private function handleFindCrimerHome(event:Event):void
		{
			var dataXML:XML = XML(event.target.data);
			processCommonResult(dataXML);
			var animals:Object = PlayerInfoHelper.getInstance().animals;
			var len:int = dataXML.animal.length();
			for (var i:int = 0; i < len; i++)
			{ 
				var animal:AnimalInfo = new AnimalInfo(dataXML.animal[i]);
				animals[animal.id] = animal;
			}
			if (dataXML.packageInfo.length() > 0)
			{
				var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
				player.userPackage = new PackageInfo(dataXML.packageInfo[0]);
			}
			var crimerOwnerId:int = parseInt(dataXML.crimerOwnerId);
			var crimerLiveId:int = parseInt(dataXML.crimerLiveId);
			BottomNavigator.getInstance().refresh();
			if (callBackFunc != null)
				this.callBackFunc(crimerOwnerId, crimerLiveId);
		}
		
//		//更新装备评分信息
//		public function updatePlayerHonour(userId:int, equipsId:int, equipsLevel:int, callBackFunc:Function = null):Boolean
//		{
//			var variables:URLVariables = new URLVariables();
//			variables.action = "updatePlayerHonour";
//			variables.userId = userId; 
//			variables.equipsId = equipsId; 
//			variables.equipsLevel = equipsLevel; 
//			return postRequest(Profile.URL_Action, variables, handleUpdatePlayerHonour, callBackFunc);
//		}
//		
//		private function handleUpdatePlayerHonour(event:Event):void
//		{
//			
//		}
	}
}